package com.tanhua.server.service;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.dubbo.api.BlackListApi;
import com.itheima.dubbo.api.QuestionApi;
import com.itheima.dubbo.api.SettingsApi;
import com.tanhua.model.domain.Question;
import com.tanhua.model.domain.Settings;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.SettingsVo;
import com.tanhua.server.interceptor.UserThreadLocal;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @Author Pan Rundong
 * @Date 2023-02-27 9:47
 * @Desc 设置表Service
 */
@Service
public class SettingsService {
    @DubboReference
    private QuestionApi questionApi;
    @DubboReference
    private SettingsApi settingsApi;
    @DubboReference
    private BlackListApi blackListApi;
    /**
     * 获取设置
     * @Author: Pan Rundong
     * @return com.tanhua.model.vo.SettingsVo
     */
    public SettingsVo getSettings() {
        SettingsVo settingsVo = new SettingsVo();
        //1.获取用户Id以及手机号
        Long userId = UserThreadLocal.getUserId();
        String phone = UserThreadLocal.getPhone();

        //2.通过Id获取Question
        Question question = questionApi.getById(userId);
        String strangerQuestion = question == null ? "Do you like Java?" : question.getTxt();

        //3.通过Id获取Settings
        Settings settings = settingsApi.getById(userId);

        //4.分装入vo
        settingsVo.setId(userId);
        settingsVo.setPhone(phone);
        settingsVo.setStrangerQuestion(strangerQuestion);
        if (settings != null) {
            settingsVo.setLikeNotification(settings.getLikeNotification());
            settingsVo.setGonggaoNotification(settings.getGonggaoNotification());
            settingsVo.setPinglunNotification(settings.getPinglunNotification());
        }

        //5.返回
        return settingsVo;
    }

    /**
     * 设置问题：
     *      如果有则更新
     *      如果没有则保存
     * @Author: Pan Rundong
     * @param map
     * @return void
     */
    public void questions(Map map) {
        //1.获取正文内容
        String content = (String) map.get("content");
        //2.获取用户Id
        Long userId = UserThreadLocal.getUserId();
        //3.查询用户问题
        Question question = questionApi.getById(userId);

        //4.判断
        if (question == null) {
            //4.1用户未设置question
            question = new Question();
            question.setUserId(userId);
            question.setTxt(content);
            questionApi.save(question);
        } else {
            //4.2用户设置了question
            question.setTxt(content);
            questionApi.update(question);
        }

    }
    /**
     * 更改用户
     * @Author: Pan Rundong
     * @param settings
     * @return void
     */
    public void update(Settings settings) {
        //1.获取用户Id
        Long userId = UserThreadLocal.getUserId();

        //2.根据用户Id查询设置信息
        Settings settingsApiById = settingsApi.getById(userId);

        //3.判断是否存在用户设置
        if (settingsApiById != null) {
            //3.1存在，更新
            settingsApi.update(settings);
        } else {
            //3.2不存在，保存
            settings.setUserId(userId);
            settingsApi.save(settings);
        }
    }

    /**
     * 获取黑名单
     * @Author: Pan Rundong
     * @return com.tanhua.model.vo.PageResult
     */
    public PageResult getBlackList(int page, int size) {
        //1.获取用户Id
        Long userId = UserThreadLocal.getUserId();

        //2.查询黑名单
        Page<UserInfo> pageInfo = blackListApi.getById(userId, page, size);

        //3.转换为PageResult
        PageResult pageResult = new PageResult(page, size, (int) pageInfo.getTotal(), pageInfo.getRecords());

        //4.返回
        return pageResult;
    }

    /**
     * 移除黑名单
     *
     * @param uid
     * @return void
     * @Author Pan Rundong
     */
    public void deleteBlackList(Long uid) {
        //1.获取当前线程的用户id
        Long userId = UserThreadLocal.getUserId();
        //2.远程调用api
        blackListApi.deleteBlackList(uid, userId);
    }
}
